package com.idrem.views.visit;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.widget.BaseAdapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public abstract class ImageListViewAdapter extends BaseAdapter {
	private final int MSG_REFRESH = 100;
	private List dataList;
	private Bitmap defaultImage;
	public HashMap<String, Bitmap> imageCache;
	private boolean isLoadingMore;
	private Context mContext;
	private ImageListViewFooterChannel mFooterChannel;
	private final Handler mHandler = new Handler() {
		public void handleMessage(Message paramMessage) {
			if (100 == paramMessage.what)
				ImageListViewAdapter.this.refreshAdapter();
			super.handleMessage(paramMessage);
		}
	};
	private ImageLoadTask mImageLoadTask;
	private LayoutInflater mLayoutInflater;
	private LoadMoreItemsTask mLoadMoreItemsTask;
	private final Queue<String> mTaskQueue = new LinkedList();
	private boolean smoothMode = false;

	public ImageListViewAdapter(Context paramContext, List paramList) {
		this.mContext = paramContext;
		this.mLayoutInflater = LayoutInflater.from(paramContext);
		this.dataList = paramList;
		this.imageCache = new HashMap();
		this.defaultImage = BitmapFactory.decodeFile(getDefaultImage());
	}

	private void mergeListData(List paramList) {
		if (paramList != null) {
			for (int j = 0; j < paramList.size(); ++j) {
				this.dataList.add(paramList.get(j));
			}
		}
	}

	private void offerTask(String[] paramArrayOfString) {
		int i = paramArrayOfString.length;
		for (int j = 0;; ++j) {
			if (j >= i)
				return;
			String str = paramArrayOfString[j];
			this.mTaskQueue.add(str);
		}
	}

	public void clearTaskQueue() {
		synchronized (this.mTaskQueue) {
			this.mTaskQueue.clear();
			return;
		}
	}

	public Bitmap doLoadImage(String paramString) {
		BitmapFactory.Options localOptions = new BitmapFactory.Options();
		localOptions.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(paramString, localOptions);
		int i = (int) (localOptions.outHeight / 78.0F);
		if (i <= 0)
			i = 1;
		localOptions.inJustDecodeBounds = false;
		localOptions.inSampleSize = i;
		return BitmapFactory.decodeFile(paramString, localOptions);
	}

	public List doloadMoreItems(int paramInt1, int paramInt2, int paramInt3) {
		return null;
	}

	public void enableSmoothMode(boolean paramBoolean) {
		this.smoothMode = paramBoolean;
	}

	public Context getContext() {
		return this.mContext;
	}

	public int getCount() {
		return getDataList().size();
	}

	public List getDataList() {
		return this.dataList;
	}

	public abstract String getDefaultImage();

	public abstract String[] getImage(int paramInt, Object paramObject);

	public Object getItem(int paramInt) {
		return getDataList().get(paramInt);
	}

	public long getItemId(int paramInt) {
		return 0L;
	}

	public LayoutInflater getLayoutInflater() {
		return this.mLayoutInflater;
	}

	public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
		return getView(paramInt, paramView, paramViewGroup, getItem(paramInt));
	}

	public abstract View getView(int paramInt, View paramView,
			ViewGroup paramViewGroup, Object paramObject);

	public boolean hasMoreData(int paramInt1, int paramInt2, int paramInt3) {
		return false;
	}

	public void initLoadQueue(int paramInt1, int paramInt2) {
	}

	public boolean isLoadingMore() {
		return this.isLoadingMore;
	}

	public boolean isSmoothMode() {
		return this.smoothMode;
	}

	public Bitmap loadDrawable(String paramString) {
		if (this.imageCache.containsKey(paramString))
			;
		for (Bitmap localBitmap = (Bitmap) this.imageCache.get(paramString);; localBitmap = null)
			return localBitmap;
	}

	public void loadImage(String paramString) {
		Bitmap localBitmap = doLoadImage(paramString);
		if (localBitmap == null)
			return;
		this.imageCache.put(paramString, localBitmap);
	}

	public void refreshAdapter() {
		notifyDataSetChanged();
	}

	public void setFooterChannel(
			ImageListViewFooterChannel paramImageListViewFooterChannel) {
		this.mFooterChannel = paramImageListViewFooterChannel;
	}

	public void startImageLoadTask() {
		synchronized (this.mTaskQueue) {
			if (this.mImageLoadTask != null)
				this.mImageLoadTask = null;
			this.mImageLoadTask = new ImageLoadTask();
			ImageLoadTask localImageLoadTask = this.mImageLoadTask;
			Queue[] arrayOfQueue = new Queue[1];
			arrayOfQueue[0] = this.mTaskQueue;
			localImageLoadTask.execute(arrayOfQueue);
			return;
		}
	}

	public void startLoadMore(int paramInt1, int paramInt2, int paramInt3) {
		if ((isLoadingMore())
				|| (!hasMoreData(paramInt1, paramInt2, paramInt3))
				|| (this.mLoadMoreItemsTask != null))
			return;
		this.mLoadMoreItemsTask = new LoadMoreItemsTask(paramInt1, paramInt2,
				paramInt3);
		this.mLoadMoreItemsTask.execute(new Void[0]);
	}

	public void stopImageLoadTask() {
		synchronized (this.mTaskQueue) {
			if (this.mImageLoadTask != null)
				this.mImageLoadTask.cancel(false);
			return;
		}
	}

	public void stopLoadMore() {
		if (this.mLoadMoreItemsTask == null)
			return;
		this.mLoadMoreItemsTask.cancel(true);
	}

	public static abstract interface ImageCallback {
		public abstract void imageLoaded(Drawable paramDrawable,
				String paramString);
	}

	protected static abstract interface ImageListViewFooterChannel {
		public abstract void hideFooterView();

		public abstract void showFooterView();
	}

	private class ImageLoadTask extends AsyncTask<Queue<String>, Void, Void> {
		private ImageLoadTask() {
		}

		protected Void doInBackground(Queue<String>[] paramArrayOfQueue) {
			return null;
		}

		protected void onCancelled() {
			ImageListViewAdapter.this.mLoadMoreItemsTask = null;
			super.onCancelled();
		}

		protected void onPostExecute(Void paramVoid) {
			ImageListViewAdapter.this.mLoadMoreItemsTask = null;
			super.onPostExecute(paramVoid);
		}
	}

	private class LoadMoreItemsTask extends AsyncTask<Void, Void, List> {
		private final int mFirstVisibleItem;
		private final int mTotalItemCount;
		private final int mVisibleItemCount;

		public LoadMoreItemsTask(int paramInt1, int paramInt2, int paramInt3) {
			this.mFirstVisibleItem = paramInt1;
			this.mVisibleItemCount = paramInt2;
			this.mTotalItemCount = paramInt3;
		}

		protected List doInBackground(Void[] paramArrayOfVoid) {
			return ImageListViewAdapter.this.doloadMoreItems(
					this.mFirstVisibleItem, this.mVisibleItemCount,
					this.mTotalItemCount);
		}

		protected void onCancelled() {
			super.onCancelled();
			ImageListViewAdapter.this.isLoadingMore = false;
			ImageListViewAdapter.this.mLoadMoreItemsTask = null;
		}

		protected void onPostExecute(List paramList) {
			super.onPostExecute(paramList);
			if ((isCancelled()) || (ImageListViewAdapter.this.dataList == null)
					|| (ImageListViewAdapter.this.mContext == null)) {
				ImageListViewAdapter.this.mergeListData(paramList);
				ImageListViewAdapter.this.refreshAdapter();
				if (ImageListViewAdapter.this.mFooterChannel != null)
					ImageListViewAdapter.this.mFooterChannel.hideFooterView();
				ImageListViewAdapter.this.isLoadingMore = false;
				ImageListViewAdapter.this.mLoadMoreItemsTask = null;
			}
		}

		protected void onPreExecute() {
			super.onPreExecute();
			ImageListViewAdapter.this.isLoadingMore = true;
			if (ImageListViewAdapter.this.mFooterChannel == null)
				return;
			ImageListViewAdapter.this.mFooterChannel.showFooterView();
		}
	}
}